// pages/chooseImg/chooseImg.js
const ctxPuzzle = wx.createCanvasContext("puzzleCanvas", this)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cameraInfo: {
      width: 550,
      height: 550,
    },
    systemInfo: {},
    photoInfo: {},
    picnumber: null
  },


  takePhoto() {
    var that = this
    that.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.getImageInfo({
          src: res.tempImagePath,
          success: res => {
            console.log(res)
            that.setData({
              photoInfo: res
            })
            let screenWidth = that.data.systemInfo.windowWidth
            let screenHeight = that.data.systemInfo.windowHeight
            let bili = screenWidth / 750
            let length = 183 * bili
            ctxPuzzle.drawImage(res.path, length, length, length * 3, length * 3, length, length, length, length)
            // ctxPuzzle.draw()
            // var timeName = setTimeout(function () {
            //   wx.canvasToTempFilePath({
            //     canvasId: 'puzzleCanvas',
            //     fileType: "png",
            //     success: function (res) {
            //       wx.hideLoading()
            //       that.setData({
            //         completeImageInfo: res.tempFilePath
            //       })
            //       wx.previewImage({
            //         urls: [res.tempFilePath],
            //       })
            //       clearTimeout(timeName)
            //     }
            //   })
            // }, 3000)

            ctxPuzzle.draw(true, setTimeout(function () {
              wx.canvasToTempFilePath({
                canvasId: 'puzzleCanvas',
                fileType: "png",
                success: function (res) {
                  let i = that.data.picnumber.data
                  if (i == "first") {
                    console.log("first")
                    wx.setStorage({
                      key: "first",
                      data:[res.tempFilePath]
                    })
                  }
                  else {
                    console.log("second")
                    wx.setStorage({
                      key: "second",
                      data:[res.tempFilePath]
                    })
                  }
                  wx.previewImage({
                    urls: [res.tempFilePath],
                  })
                  
                }
              })
            }, 1000));



            // ctxPuzzle.drawImage(res.path, 0, res.height * that.data.cameraInfo.top / that.data.systemInfo.windowHeight, res.width, res.height * that.data.cameraInfo.height / that.data.systemInfo.windowHeight, 0, 0, that.data.cameraInfo.width, that.data.cameraInfo.height)
            // ctxPuzzle.drawImage(that.data.frameImageInfo.path, 0, 0, that.data.frameImageInfo.width, that.data.frameImageInfo.height, 0, 0, that.data.cameraInfo.width, that.data.cameraInfo.height)
            // ctxPuzzle.draw()

            // var timeName = setTimeout(function () {
            //     wx.canvasToTempFilePath({
            //         canvasId: 'puzzleCanvas',
            //         fileType: "png",
            //         success: function (res) {
            //             // wx.hideLoading()
            //             // that.setData({
            //             //   completeImageInfo: res.tempFilePath
            //             // })
            //             wx.previewImage({
            //                 urls: [res.tempFilePath],
            //             })
            //             clearTimeout(timeName)
            //         }
            //     })
            // }, 5000)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.query)
    var that = this
    const eventChannel = this.getOpenerEventChannel()
    // 向调用页面传值
    // eventChannel.emit('acceptDataFromOpenedPage', { data: 'tuyi' });
    // eventChannel.emit('someEvent', { data: 'tuer' });

    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      // 这里的data中first代表选择第一张图片，second代表选择第二张图片
      console.log(data)
      that.setData({
        picnumber: data
      })
    })
    // 获取相机上下文
    that.ctx = wx.createCameraContext()
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          systemInfo: res
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})